package com.bajiao.app.controller.coupon;

import cn.hutool.core.util.StrUtil;
import com.bajiao.app.base.AppBaseController;
import com.bajiao.common.api.vo.RestResult;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.web.entity.TableDataInfo;
import com.bajiao.service.coupon.CouponService;
import com.bajiao.vo.coupon.CouponDetailsResponse;
import com.bajiao.vo.coupon.CouponResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = "优惠券接口")
@RestController
@RequestMapping("/api/v1/coupon")
public class RestCouponController extends AppBaseController {

    @Resource
    private CouponService couponService;

    @GetMapping(value = "/store/page")
    @ApiOperation(value = "商家专用劵分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "storeId", value = "商家id", dataType = "String", required = true),
    })
    public RestResult<TableDataInfo<CouponResponse>> getStoreCouponPage(@ModelAttribute("userId") String userId,
                                                                        @RequestParam("storeId") String storeId) {
        startPage();
        List<CouponResponse> couponList = couponService.getByStoreId(userId, storeId);
        return success(getDataTable(couponList));
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "优惠券分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "type", value = "0-全部 1-专属券 2-折扣券 3-抵扣券", dataType = "Integer", required = true),
    })
    public RestResult<TableDataInfo<CouponResponse>> getCouponPage(@ModelAttribute("userId") String userId,
                                                                   @RequestParam("type") Integer type) {
        startPage();
        List<CouponResponse> couponList = couponService.getPage(userId, type);
        return success(getDataTable(couponList));
    }

    @GetMapping(value = "/order/usable")
    @ApiOperation(value = "订单可用劵分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "storeId", value = "商家id", dataType = "String", required = true),
    })
    public RestResult<TableDataInfo<CouponResponse>> getOrderCouponPage(@ModelAttribute("userId") String userId,
                                                                        @RequestParam("storeId") String storeId) {
        if (StrUtil.isBlank(userId)) return tokenOverdue();
        startPage();
        List<CouponResponse> couponList = couponService.getOrderCouponPage(userId, storeId);
        return success(getDataTable(couponList));
    }

    @GetMapping(value = "/details")
    @ApiOperation(value = "优惠券详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "String", required = true)
    })
    public RestResult<CouponDetailsResponse> getCouponDetails(@ModelAttribute("userId") String userId,
                                                              @RequestParam("couponId") String couponId) {
        CouponDetailsResponse response = couponService.getDetails(userId, couponId);
        return success(response);
    }

    /**
     * @description: 首页-热门推荐-指定优惠劵可使用商家
     * @author: wangw
     * @date: 2025.03.03
     */
    @ApiOperation(value = "指定优惠劵可使用商家")
    @GetMapping(value = "/getUseableStoreList")
    public Result<?> getUseableStoreList(@RequestParam("couponId") String couponId, @RequestParam("pageNumber") Integer pageNumber, @RequestParam("pageSize") Integer pageSize) {
        return Result.ok(couponService.getUseableStoreList(couponId,pageNumber,pageSize));
    }
}
